package leetcode.D200.T123;

import java.util.Arrays;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-02-22 15:14:55 星期二
 */
class Solution {
    public int maxProfit(int[] prices) {
        int[] dp0 = new int[3], dp1 = new int[3];
        Arrays.fill(dp1, -prices[0]);
        for(int i=1; i<prices.length; ++i) {
            int last = dp0[1];
            dp0[1] = Math.max(dp0[1], dp1[1]+prices[i]);
            dp1[1] = Math.max(dp1[1], -prices[i]);
            dp0[2] = Math.max(dp0[2], dp1[2]+prices[i]);
            dp1[2] = Math.max(dp1[2], last-prices[i]);
        }
        return Math.max(dp0[1], dp0[2]);
    }
}
